﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using FluentValidation;
using FluentValidation.Attributes;
using PicStore.Common;

namespace PicStore.Web.Models.User
{
    [Validator(typeof(UserModelValidator))]
    public class UserModel
    {
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm Password")]
        public string ConfirmPassword { get; set; }

        [Display(Name = "Email")]
        public string Email { get; set; }

        [Display(Name = "Phone")]
        public string Phone { get; set; }
        [Display(Name = "Mobile")]
        public string Mobile { get; set; }
        [Display(Name = "Department")]
        public string Department { get; set; }

        [Display(Name = "Role")]
        public int RoleId { get; set; }

        public int Id { get; set; }
        public string CreateDate { get; set; }
        public bool IsDeleted { get; set; }

        [Display(Name = "Role")]
        public string Role { get; set; }

        [Display(Name = "Role")]
        public List<System.Web.Mvc.SelectListItem> RoleList { get; set; }

        public bool IsValid { get; set; }
        public string ErrorMessage { get; set; }

        public static UserModel BuildUserModel(Model.User user)
        {
            var model = new UserModel()
            {
                Id = user.UserId,
                UserName = user.UserName,
                CreateDate = user.CreateDate.ToShortDateString(),
                Email = user.Email,
                Department = user.Department,
                Phone = user.Phone,
                Mobile = user.Mobile,
                IsDeleted = user.IsDeleted,
                RoleId = user.Role.Id,
                Role = user.Role.Name,
            };

            return model;
        }

        public static Model.User BuildUserByUserModel(UserModel user)
        {
            var model = new Model.User()
            {
                UserId = user.Id,
                UserName = user.UserName,
                CreateDate = DateTime.Parse(user.CreateDate),
                Email = user.Email,
                Department = user.Department,
                Phone = user.Phone,
                Mobile = user.Mobile,
                IsDeleted = user.IsDeleted,
            };

            model.Role = new Model.Role()
            {
                Id = user.RoleId,
                Name = user.Role,
            };

            return model;
        }
    }

    public class UserModelValidator : AbstractValidator<UserModel>
    {
        public UserModelValidator()
        {
            int lengthLimit = 6;
            RuleFor(x => x.UserName).NotNull().WithMessage(string.Format(Constants.MustInput, "User Name"));
            RuleFor(x => x.Password).NotNull().Must(x => x.Length >= lengthLimit)
                .WithMessage(string.Format(Constants.MinLength, "Password", lengthLimit));
            RuleFor(x => x.ConfirmPassword).NotNull().WithMessage(Constants.PwdMismatch);
            RuleFor(x => x.ConfirmPassword).Equal(x => x.Password).WithMessage(Constants.PwdMismatch);
            RuleFor(x => x.Email).EmailAddress().NotNull().WithMessage(string.Format(Constants.MustInput, "E-mail"));
        }
    }
}